package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.service.StoreService;
import com.tarena.lbs.common.basic.converter.StoreConverter;
import com.tarena.lbs.pojo.basic.bo.StoreBO;
import com.tarena.lbs.pojo.basic.param.StoreParam;
import com.tarena.lbs.pojo.basic.param.UserLocationParam;
import com.tarena.lbs.pojo.basic.query.AreaStoreQuery;
import com.tarena.lbs.pojo.basic.query.StoreQuery;
import com.tarena.lbs.pojo.basic.vo.StoreVO;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class StoreController {
    @Autowired
    private StoreService storeService;
    @Autowired
    private StoreConverter storeConverter;
    //后台店铺分页查询
    @PostMapping("/admin/basic/store/getAll")
    public Result<PageResult<StoreVO>> storeList(@RequestBody StoreQuery query){
        //业务层返回 basePageList total objects
        BasePageList<StoreBO> boPages = storeService.storeList(query);
        PageResult<StoreVO> pageVOS = storeConverter.assemblePageVOS(boPages, query);
        return new Result<>(pageVOS);

    }
    //新增门店 店铺数据
    @PostMapping("/admin/basic/store/add")
    public Result<Void> saveStore(@RequestBody StoreParam storeParam) throws BusinessException, IOException {
        storeService.saveStore(storeParam);
        return Result.success();
    }

    @PostMapping("/admin/basic/store/getStoreByCity")
    public Result<PageResult<StoreVO>> getCitiesStores(@RequestBody AreaStoreQuery query) throws BusinessException{
        BasePageList<StoreBO> storeBOS = storeService.getCitiesStores(query);
        PageResult<StoreVO> storeVOPageResult = storeConverter.assemblePageVOS(storeBOS, query);
        return new Result<>(storeVOPageResult);
    }

    @PostMapping("/basic/store/location")
    public Result<StoreVO> locationReport(@RequestBody UserLocationParam param)
            throws BusinessException{
        StoreBO bo = storeService.locationReport(param);
        if (bo!=null){
            StoreVO vo = new StoreVO();
            BeanUtils.copyProperties(bo,vo);
            return new Result<>(vo);
        }
        return null;
    }

}
